/**
 * *************************************
 * Copyright(c)2016 Dingdang Yixia 
 * All Rights Reserved.
 *
 * @File: SimpleHttpPost.java
 * @Package: com.ddyx.snapface.common
 * @Description:
 * 
 * @Version: V1.0
 * @Author: Yidong.Xiang
 * @Date: 2017年9月22日 下午7:31:21
 * 
 * *************************************
 */
package com.ddyx.common;

import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @ClassName: SimpleHttpPost.java
 * @Module: 
 * @Description: 
 * 
 * @author: Yidong.Xiang
 * @date: 2017年9月22日 下午7:31:21
 * 
 */

public class SimpleHttpPost
{
  private static SimpleHttpPost instance = null;
  
  public static SimpleHttpPost getInstance()
  {
    if (instance == null) {
      instance = new SimpleHttpPost();
    }
    return instance;
  }
  
  public String postJson(String url, JSONObject param)
  {
    if ((url == null) || ("".equals(url))) {
      return null;
    }
    if (param == null) {
      return null;
    }
    HttpClient httpClient = HttpClients.createDefault();
    HttpPost request = null;
    CloseableHttpResponse response = null;
    try
    {
      request = new HttpPost(url);
      StringEntity entity = new StringEntity(param.toString(), "UTF-8");
      entity.setContentEncoding("UTF-8");
      entity.setContentType("application/json");
      request.setEntity(entity);
      response = (CloseableHttpResponse)httpClient.execute(request);
      String body = EntityUtils.toString(response.getEntity(), "UTF-8");
      response.close();
      return body;
    }
    catch (ParseException e)
    {
      e.printStackTrace();
      return null;
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  public String postForm(String url, JSONObject param)
  {
    if ((url == null) || ("".equals(url))) {
      return null;
    }
    if (param == null) {
      return null;
    }
    HttpClient httpClient = HttpClients.createDefault();
    HttpPost request = null;
    CloseableHttpResponse response = null;
    try
    {
      request = new HttpPost(url);
      List<NameValuePair> foramParams = new ArrayList<NameValuePair>();
      for (String key : param.keySet())
      {
        Object obj = param.get(key);
        String value = obj == null ? "" : obj.toString();
        foramParams.add(new BasicNameValuePair(key, value));
      }
      UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(foramParams, "UTF-8");
      urlEntity.setContentEncoding("UTF-8");
      request.setEntity(urlEntity);
      response = (CloseableHttpResponse)httpClient.execute(request);
      String body = EntityUtils.toString(response.getEntity());
      response.close();
      return body;
    }
    catch (ParseException e)
    {
      e.printStackTrace();
      return null;
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return null;
  }
}